import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ClientInputPanel extends javax.swing.JPanel implements ActionListener {
	private JTextField clientNameField;
	private JLabel clientNameLabel;
	private JButton connectButton;
	private JButton colorButton;
	private JLabel colorLabel;

	private WhiteBoardClient client;
	

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ClientInputPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public ClientInputPanel() {
		super();
		initGUI();
	}

	public ClientInputPanel(WhiteBoardClient client) {
		super();
		initGUI();
		this.client = client;
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent) this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(385, 110));
			{
				clientNameField = new JTextField();
				clientNameField.setText("client" + String.valueOf((int)(Math.random()*1000)));
				clientNameField.setActionCommand("NAME");
				clientNameField.addActionListener(this);
			}
			{
				clientNameLabel = new JLabel();
				clientNameLabel.setText("Client Name: ");
			}
			{
				connectButton = new JButton();
				connectButton.setText("Connect");
				connectButton.setActionCommand("CONNECT");
				connectButton.addActionListener(this);
			}
			{
				colorLabel = new JLabel();
				colorLabel.setText("Client Color: ");
			}
			{
				colorButton = new JButton();
				colorButton.setText("Color");
				colorButton.setActionCommand("COLOR");
				colorButton.addActionListener(this);

			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(clientNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(clientNameLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(colorButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(colorLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(connectButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(colorLabel, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
				        .addGap(32))
				    .addComponent(clientNameLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(clientNameField, GroupLayout.Alignment.LEADING, 0, 226, Short.MAX_VALUE)
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(139)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGap(0, 0, Short.MAX_VALUE)
				                .addComponent(colorButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
				            .addComponent(connectButton, GroupLayout.Alignment.LEADING, 0, 87, Short.MAX_VALUE))))
				.addContainerGap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "COLOR") {
			final JColorChooser ch = new JColorChooser(client.getColor());
			ch.getSelectionModel().addChangeListener(new ChangeListener() {

				@Override
				public void stateChanged(ChangeEvent arg0) {
					client.setColor(ch.getSelectionModel().getSelectedColor());

				}

			});
			JDialog d = new JDialog();
			d.setSize(new Dimension(400, 400));
			d.setLocationRelativeTo(null);
			d.add(ch);
			d.setModal(true);
			d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			d.setVisible(true);
			
		} else if (e.getActionCommand() == "NAME") {
			client.setName(clientNameField.getText());
			
		} else if(e.getActionCommand() == "CONNECT"){
			client.setName(clientNameField.getText());
			client.connect();
		}

	}

}
